# _*_ coding : utf-8 _*_
# @Time : 2023/3/14 0014 22:34
# @Author : 菜鸟王小二
# @File : 6_urllib_post请求百度翻译
# @Project : python爬虫
import json
import urllib.request
import urllib.parse

url = 'https://fanyi.baidu.com/sug'
data = {
    'kw':'测试'
}
data = urllib.parse.urlencode(data).encode('utf-8')
print(data)
headers = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}
# data必须为字节形式，所以上面的data后面加了encode('utf-8')
request = urllib.request.Request(url=url,data=data,headers=headers)

response = urllib.request.urlopen(request)

text = response.read().decode('utf-8')

# 使用json.loads()方法将这些形式：\u8bd5\u9a8c; \u6d4b\u9a8c 的数据转为中文
result = json.loads(text)

print(result)